package cn.uppower.wordaliy.fragment;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.List;

import cn.uppower.wordaliy.R;
import cn.uppower.wordaliy.activity.TestActivity;
import cn.uppower.wordaliy.model.PageWord;
import cn.uppower.wordaliy.model.Words;

/**
 * Created by elitetyc on 2018/12/14.
 * Describe:
 */
@SuppressLint("ValidFragment")
public class TestFeagment extends Fragment {

    private Context context;
    private PageWord words;
    private int trueIndex;

    private TextView tv_words,tv_words_desc1,tv_words_desc2,tv_words_desc3,tv_words_desc4;
    private RelativeLayout rl_option1,rl_option2,rl_option3,rl_option4;
    private ImageView iv_word_image_boolean1,iv_word_image_boolean2,iv_word_image_boolean3,iv_word_image_boolean4;

    public TestFeagment(PageWord words,int trueIndex) {
        this.words = words;
        this.trueIndex = trueIndex;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        context=getActivity();
        View view= View.inflate(context, R.layout.fragment_test,null);
        tv_words = view.findViewById(R.id.tv_words);
        rl_option1 = view.findViewById(R.id.rl_option1);
        rl_option2 = view.findViewById(R.id.rl_option2);
        rl_option3 = view.findViewById(R.id.rl_option3);
        rl_option4 = view.findViewById(R.id.rl_option4);
        tv_words_desc1 = view.findViewById(R.id.tv_words_desc1);
        tv_words_desc2 = view.findViewById(R.id.tv_words_desc2);
        tv_words_desc3 = view.findViewById(R.id.tv_words_desc3);
        tv_words_desc4 = view.findViewById(R.id.tv_words_desc4);
        iv_word_image_boolean1 = view.findViewById(R.id.iv_word_image_boolean1);
        iv_word_image_boolean2 = view.findViewById(R.id.iv_word_image_boolean2);
        iv_word_image_boolean3 = view.findViewById(R.id.iv_word_image_boolean3);
        iv_word_image_boolean4 = view.findViewById(R.id.iv_word_image_boolean4);

        tv_words.setText(words.getCorrectWords().getEnglish());
        switch (trueIndex){
            case 1:
                tv_words_desc1.setText(words.getCorrectWords().getChinese());
                tv_words_desc2.setText(words.getErrorOption1());
                tv_words_desc3.setText(words.getErrorOption2());
                tv_words_desc4.setText(words.getErrorOption3());
                break;
            case 2:
                tv_words_desc2.setText(words.getCorrectWords().getChinese());
                tv_words_desc1.setText(words.getErrorOption1());
                tv_words_desc3.setText(words.getErrorOption2());
                tv_words_desc4.setText(words.getErrorOption3());
                break;
            case 3:
                tv_words_desc3.setText(words.getCorrectWords().getChinese());
                tv_words_desc2.setText(words.getErrorOption1());
                tv_words_desc1.setText(words.getErrorOption2());
                tv_words_desc4.setText(words.getErrorOption3());
                break;
            case 4:
                tv_words_desc4.setText(words.getCorrectWords().getChinese());
                tv_words_desc2.setText(words.getErrorOption1());
                tv_words_desc3.setText(words.getErrorOption2());
                tv_words_desc1.setText(words.getErrorOption3());
                break;
        }

        rl_option1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                reSetAll();
                iv_word_image_boolean1.setVisibility(View.VISIBLE);
                TestActivity.userSelect.put(words.getCorrectWords().getId(),1);
            }
        });

        rl_option2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                reSetAll();
                iv_word_image_boolean2.setVisibility(View.VISIBLE);
                TestActivity.userSelect.put(words.getCorrectWords().getId(),2);
            }
        });

        rl_option3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                reSetAll();
                iv_word_image_boolean3.setVisibility(View.VISIBLE);
                TestActivity.userSelect.put(words.getCorrectWords().getId(),3);
            }
        });

        rl_option4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                reSetAll();
                iv_word_image_boolean4.setVisibility(View.VISIBLE);
                TestActivity.userSelect.put(words.getCorrectWords().getId(),4);
            }
        });
        return view;
    }

    private void reSetAll() {
        iv_word_image_boolean1.setVisibility(View.GONE);
        iv_word_image_boolean2.setVisibility(View.GONE);
        iv_word_image_boolean3.setVisibility(View.GONE);
        iv_word_image_boolean4.setVisibility(View.GONE);
    }
}
